/*
 * Alexey Danchuk
 * This software is an implementation of the second task from tasks.pdf
 */
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;

/**
 * Program to find two neighbouring numbers in an array with the smallest
 * distance to each other.
 * 
 * @version	1.01 27 January 2013
 * @author 	Alexey Danchuk
 */
public class Neighborhood {

	/**
	 * Method for getting array size from user.
	 * 
	 * @throws Exception
	 */
	public static int[] inputSize() throws Exception {
		BufferedReader inputStream = new BufferedReader(new InputStreamReader(
				                                            System.in));
		System.out.println("Enter array size: ");
		int inputNumber = Integer.parseInt(inputStream.readLine());
		int[] numbers = new int[inputNumber];
		return numbers;
	}

	/**
	 * Method for filling an array.
	 * 
	 * @return inNumbers - our filled array
	 */
	public static int[] initArray() {
		int[] inNumbers = null;
		try {
			inNumbers = inputSize();
		} catch (Exception e) {
			e.printStackTrace();
		}
		/* Simple checking to continue entering values */
		if (inNumbers != null) {
			Scanner inScanner = new Scanner(System.in);
			System.out.println("Enter array elements: ");
			/* Filling our array with next entered integer value */
			for (int i = 0; i < inNumbers.length; i++) {
				inNumbers[i] = inScanner.nextInt();
			}

			inScanner.close();
		} else {
			System.err.println("Array problems!");
		}
		return inNumbers;
	}

	/**
	 * Method for neighbor find.
	 * 
	 * @param numbers - array to search
	 * @return interimResult[1] - the index of the first number with the
	 *         smallest distance
	 */
	public static int findNeighbor(int[] numbers) {
		/* For smallest distance (0 distance) and first number index (1 index). */
		int[] interimResult = new int[2];
		int smallestDistance = numbers[1] - numbers[0];
		/*
		 * Starts with 1 because we want to avoid
		 * ArrayIndexOutOfBoundsException.
		 */
		for (int i = 1; i < numbers.length; i++) {
			if (((numbers[i] - numbers[i - 1]) < smallestDistance)
					&& ((numbers[i] - numbers[i - 1]) > 0)) {
				smallestDistance = (numbers[i] - numbers[i - 1]);
				/* If we want to output this in the next version. */
				interimResult[0] = smallestDistance;
				interimResult[1] = i - 1;
			}
		}
		return interimResult[1];
	}

	/**
	 * Method for output results.
	 * 
	 * @param numbers - inputed array
	 */
	public static void output(int[] numbers) {
		System.out.print("Inputed array is: ");
		for (int number : numbers) {
			System.out.print(number + " ");
		}
		System.out.println("\nThe index of the first number \nwith smallest distance between next neighbor is: ");
		/* + 1 because indexing starts from zero. For user only. */
		System.out.print(findNeighbor(numbers) + 1);
	}

	/**
	 * Main method. Entry point into the program.
	 * 
	 * @param args - command-Line arguments, this allows the user to specify
	 * configuration information when the application is launched.
	 */
	public static void main(String[] args) {
		int[] num = initArray();
		output(num);
	}
}
